//
//  WQ_ServerApi.swift
//  xinlang
//
//  Created by 联创—王增辉 on 2019/3/25.
//  Copyright © 2019年 lcWorld. All rights reserved.
//

import Moya

enum WQ_ServerApi {
    case access_token(code: String) //获取token
    case home_timeline //获取当前登录用户及其所关注（授权）用户的最新微博
}

extension WQ_ServerApi: TargetType {
    var baseURL: URL {
        switch self {
        case .access_token:
            return URL.init(string: SinaAccess_token)!
        case .home_timeline:
            return URL.init(string: SinaHome_timeline)!
        default:
            return URL.init(string:"")!
        }
    }
    
    var path: String {
        return ""
    }
    
    var method: Method {
        switch self {
        case .access_token:
            return .post
        default:
            return .get
        }
    }
    
    var sampleData: Data {
        return Data()
    }
    
    var task: Task {
        var params = [String:Any]()
        params["access_token"] = WQ_accessTokenModel.accessTokenModel.access_token == nil ? nil: WQ_accessTokenModel.accessTokenModel.access_token
        switch self {
        case .access_token(let code):
            
            params["client_id"] = SinaAppKey
            params["client_secret"] = SinaAppSecret
            params["grant_type"] = "authorization_code"
            params["code"] = code
            params["redirect_uri"] = SinaRedirectUri
            
            return .requestParameters(parameters: params, encoding: URLEncoding.default)
        case .home_timeline:
            return .requestParameters(parameters: params, encoding: URLEncoding.default)
        default:
            return .requestPlain
        }

    }
    
    var headers: [String : String]? {
        return ["Content-type": "application/x-www-form-urlencoded;charset=utf-8"]
    }
}
